package com.example.iot.utils;

//import fi.iki.elonen.NanoHTTPD;

import android.os.Environment;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

import fi.iki.elonen.NanoHTTPD;

public class HttpService extends NanoHTTPD {

    public HttpService(int port) {
        super(port);
    }

//    public Response serve(String uri, Method method, Map<String, String> headers, Map<String, String> parms, Map<String, String> files) {
//        if (method.equals(Method.GET)) {
//
//            File dst = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() +"/"+ parms.get("myfile"));
//            File src = new File(files.get("myfile"));
//            try {
//                Utils.copy(src, dst);
//            }catch (Exception e){ e.printStackTrace();}
//            return get(uri, method, headers, parms, files);
//        }

//        if(uri.equals("/downfile")) {
//            File dst = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/" + parms.get("myfile"));
//            File src = new File(files.get("myfile"));
//            try {
//                Utils.copy(src, dst);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }else if(uri.equals("/upfile")){
////            File dst = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/" + parms.get("myfile"));
////            OutputStream out = new OutputStream(dst);
//        }
//
//        return post(uri, method, headers, parms, files);
//    }

    public Response get(String uri, Method method, Map<String, String> headers, Map<String, String> parms, Map<String, String> files) {
        String get ="<html><body><form name='up' method='post' enctype='multipart/form-data'>"
                +"<input type='file' name='file' /><br /><input type='submit'name='submit'"
                +"value='Upload'/></form></body></html>";
        return NanoHTTPD.newFixedLengthResponse(get);
    }

    public Response post(String uri, Method method, Map<String, String> headers, Map<String, String> parms, Map<String, String> files) {
        String post ="<html><body>Upload successfull</body></html>";
        return NanoHTTPD.newFixedLengthResponse(post);

    }

//    //构造函数 赋值父类
//    public HttpService(int port){
//        super(port);
//    }
//
    //重写Serve方法，每次请求时会调用该方法
    @Override
    public Response serve(IHTTPSession session) {

        String uri = null;
        //获取请求uri
        if(session.getUri().equals("/test")){
            uri = "test 成功";
        } else if(session.getUri().equals("/login")){







            uri = "success";// /test16565_1,test_2
        }
        //这里默认把接收到的uri返回
        return NanoHTTPD.newFixedLengthResponse(uri);
    }
}

